#include "sprite.h"

Texture::Texture(const std::string &file_path) {
    texture = Game::GetInstance().getResourceStore()->getTexture(file_path); // 加载纹理
    SDL_GetTextureSize(texture, &src_rect.w, &src_rect.h); // 获取纹理大小
}

Sprite *Sprite::addSpriteChild(ObjectScreen *parent, const std::string &file_path, float scale, Anchor anchor) {
    const auto sprite = new Sprite(); // 创建精灵对象
    sprite->init();
    sprite->setTexture(Texture(file_path)); // 设置纹理
    sprite->setScale(scale); // 设置缩放
    sprite->setParent(parent); // 设置父对象
    parent->addChild(sprite); // 添加到父对象
    return sprite;
}

void Sprite::render() {
    if (!texture_.texture || !parent_ || is_finished_) {
        return;
    }
    const auto pos = parent_->getRenderPos() + offset_; // 获取渲染位置
    game_.renderTexture(texture_, pos, size_, percentage_); // 渲染纹理
}

void Sprite::setTexture(const Texture &texture) {
    texture_ = texture;
    size_ = glm::vec2(texture_.src_rect.w, texture_.src_rect.h); // 设置大小
}
